home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmtokens.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  2.5 KB  |  114 lines

  1. // CmTokens.cpp
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // String tokenizer implementation.
  7. // -----------------------------------------------------------------
  8.  
  9. #include <cm/include/cmtokens.h>
  10.  
  11.  
  12. // "CmTokens" is the tokenizer constructor.
  13. //
  14. CmTokens::CmTokens(const char* str)
  15. {
  16.   _string = str;
  17.   _place  = _string.text();
  18.   _idx    = 0;
  19. }
  20.  
  21.  
  22. // "CmTokens" is the tokenizer copy constructor.
  23. //
  24. CmTokens::CmTokens(const CmTokens& tk)
  25. {
  26.   _string = tk._string;
  27.   _place  = _string.text();
  28.   _idx    = 0;
  29. }
  30.  
  31.  
  32. // "=" assignment operator copies the specified tokenizer into this
  33. // tokenizer.
  34. //
  35. CmTokens& CmTokens::operator=(const CmTokens& tk)
  36. {
  37.   if (&tk != this)
  38.   {
  39.     _string = tk._string;
  40.     _place  = _string.text();
  41.     _idx    = 0;
  42.   }
  43.   return *this;
  44. }
  45.  
  46.  
  47. // "next" searches for and returns the next token in the string using
  48. // the input char string as the delimiter.
  49. //
  50. const char* CmTokens::next(const char *del)
  51. {
  52.   if (!*_place) return CmString("");
  53.  
  54.   _idx   += strspn(_place, del);
  55.   _place += strspn(_place, del);
  56.  
  57.   if (!*_place) return CmString("");
  58.  
  59.   const char* s = strpbrk(_place, del);
  60.   unsigned extent = s ? s - _place : strlen(_place);
  61.  
  62.   int start = _idx;
  63.   int stop  = _idx + extent - 1;
  64.  
  65.   _place += extent;
  66.   _idx   += extent;
  67.   return (const char*) CmString(_string(start, stop));
  68. }
  69.  
  70.  
  71. // "reset" resets the tokenizer to the beginning of the string.
  72. //
  73. void CmTokens::reset()
  74. {
  75.   _place = _string.text();
  76.   _idx   = 0;
  77. }
  78.  
  79.  
  80. // "isEqual" compares the string with the input tokenizer string.
  81. //
  82. Bool CmTokens::isEqual(CmObject* pObj) const
  83. {
  84.   if (!pObj->isA("CmTokens")) return CmObject::isEqual(pObj);
  85.   CmTokens& tk = (CmTokens&) *pObj;
  86.   return (_string.isEqual(&(tk._string)));
  87. }
  88.  
  89.  
  90. // "compare" compares the string with the input tokenizer string.
  91. //
  92. int CmTokens::compare(CmObject* pObj) const
  93. {
  94.   if (!pObj->isA("CmTokens")) return CmObject::compare(pObj);
  95.   CmTokens& tk = (CmTokens&) *pObj;
  96.   return (_string.compare(&(tk._string)));
  97. }
  98.  
  99.  
  100. // "hash" hashes on the string.
  101. //
  102. unsigned CmTokens::hash(unsigned m) const
  103. {
  104.   return _string.hash(m);
  105. }
  106.  
  107.  
  108. // "printOn" prints the string to the specified output stream.
  109. //
  110. void CmTokens::printOn(ostream& os) const
  111. {
  112.   _string.printOn(os);
  113. }
  114.